<template>
  <div class="w">

    <!-- 标题 -->
    <div class="title-area">
      <span>
        项目过程信息
      </span>
    </div>

    <!-- 主体内容显示区 -->
    <div class="content-area">

      <!-- 查询表单区 -->
      <div class="form-area">
        <Form
          name="form"
          ref="formRef"
          :labelCol="{span: 5, offset: 0}"
          @finish="formFinish"
          :initialValues="queryForm">

          <Row>
            <Col :span="8">
              <FormItem name="itemNumber" label="项目编号" initialValue="">
                <Input placeholder="请输入项目编号" style="width: 70%" />
              </FormItem>
            </Col>
            <Col :span="8">
              <FormItem name="itemName" label="项目名称" initialValue="">
                <Input placeholder="请输入项目名称" style="width: 70%" />
              </FormItem>
            </Col>
            <Col :span="8">
              <FormItem name="itemType" label="项目类型" initialValue="" style="position: relative;" class="selectPosition">
                <Select placeholder="请选择项目类型" style="width: 70%" allowClear :getPopupContainer="getPopupContainer">
                  <Option v-for="type in dict.type.item_type" :key="type.value" :label="type.label" :value="type.value"></Option>
                </Select>
              </FormItem>
            </Col>
            <Col :span="8">
              <FormItem name="itemDegree" label="项目密级" initialValue="" style="position: relative;" class="selectPosition">
                <Select placeholder="请选择项目密级" style="width: 70%" allowClear :getPopupContainer="getPopupContainer">
                  <Option v-for="type in dict.type.item_degree" :key="type.value" :label="type.label" :value="type.value"></Option>
                </Select>
              </FormItem>
            </Col>
            <Col :span="8">
              <FormItem name="dbType" label="源库类型" initialValue="" style="position: relative;" class="selectPosition">
                <Select placeholder="请选择源库类型" style="width: 70%" allowClear :getPopupContainer="getPopupContainer">
                  <Option v-for="type in dict.type.db_type" :key="type.value" :label="type.label" :value="type.value"></Option>
                </Select>
              </FormItem>
            </Col>
            <Col :span="8">
              <FormItem :wrapperCol="{span: 20, offset: 5}">
                <Button type="primary" @click="formQuery">
                  <Icon name="search" style="margin-top: 3px; margin-right: 5px;" /> 查 询
                </Button>
                <Button style="margin-left: 10px" @click="formReset">
                  <Icon name="rollback-outline" style="margin-top: 3px; margin-right: 5px;" /> 重 置
                </Button>
              </FormItem>
            </Col>
          </Row>  
        </Form>
      </div>

      <!-- 分割线 -->
      <Divider />

      <!-- 按钮区 -->
      <!-- <div class="button-area">
        <Button type="primary" @click="tblOptForm()">
          <Icon name="add" /> 新增
        </Button>
      </div> -->

      <!-- 列表区 -->
      <Skeleton
        style="margin-top: 15px;"
        :loading="isDataLoading"
        :avatar="false"
        :paragraph="{ rows: 15 }"
        active>

        <Table
          style="margin-top: 20px"
          :columns="tableColumns"
          :dataSource="tableData"
          :bordered="false"
          :rowKey="(record) => record.key"
          :scroll="{ y: 470 }"
          @change="handleTableChange"
          :pagination="{
            total: total,
            showQuickJumper: true,
            showSizeChanger: true,
            pageSize: queryForm.pageSize,
            current: queryForm.pageNum,
          }">

          <template #render="render">
            <div v-if="render.dataIndex == 'dbType'">
              <span>
                <dict-tag :options="dict.type.db_type" :value="render.record.dbType" />
              </span>
            </div>
            <div v-else-if="render.dataIndex == 'currentProgress'">
              <Tag :color="render.record.currentProgress == '已完成' ? 'green' : 'red'">
                {{ render.record.currentProgress }}
              </Tag>
            </div>
            <div v-else-if="render.dataIndex == 'operation'">
              <a href="#" @click.prevent="tblOptForm(render.record)">
                <Icon name="edit" style="margin-top: 4px;" /> 进度维护
              </a>
            </div>
          </template>
          <template #paginationTotal="props">
            <span>总共 {{ props.total }} 条</span>
          </template>
        </Table>
      </Skeleton>
    </div>

    <!-- 表单页对话框 -->
    <Modal
      class="fromModal"
      :title="formDialogInfo.title"
      v-model="formDialogInfo.visible"
      @cancel="formDialogInfo.visible = false"
      width="1100px"
      :bodyStyle="{ padding: '0px' }"
      :footer="[null]">

      <step-form
        :key="Date.now()" 
        :itemId="formDialogInfo.itemId" 
        :currRow="formDialogInfo.currRow" 
        :refData="{
          dbTypes: dict.type.db_type,
          producesList: producesList,
          resourceList: resourceList,
          solutionsList: solutionsList,
          itemDetail: itemDetail
        }" 
        @handleCancel="formDialogCancel" />
    </Modal>
  </div>
</template>

<script>
import { Message } from '@uni/vue';
import { listStep, getStep } from "@/api/doitem/itemstep";
import stepForm from "./form.vue"

export default {
  name: "app",
  dicts: ['item_type', 'db_type', 'item_degree', 'db_libraries', 'client_nature'],
  components: {
    stepForm
  },
  data: function () {
    return {
      producesList: [],
      resourceList: [],
      solutionsList: [],
      formDialogInfo: {
        title: '',
        visible: false,
        itemId: 0,
        currRow: {}
      },
      itemDetail: {},
      isDataLoading: false,
      queryForm: {
        itemNumber: '',
        itemName: '',
        itemType: '',
        itemDegree: '',
        dbType: ''
      },
      tableColumns: [
        // {"title":"序号", "dataIndex":"itemId", "key":"itemId", "align":"center", width:50},
        {"title":"项目编号", "dataIndex":"itemNumber", "key":"itemNumber", "align":"center", width:140},
        {"title":"项目名称", "dataIndex":"itemName", "key":"itemName", "align":"center"},
        {"title":"使用硬件", "dataIndex":"clientNature", "key":"clientNature", "align":"center", width:80},
        {"title":"委托单位", "dataIndex":"client", "key":"client", "align":"center"},
        {"title":"源库类型", "dataIndex":"dbType", "key":"dbType", "align":"center", width:100},
        {"title":"所处阶段", "dataIndex":"currentStage", "key":"currentStage", "align":"center"},
        {"title":"当前进度", "dataIndex":"currentProgress", "key":"currentProgress", "align":"center", width:100},
        {"title":"项目预期工期", "dataIndex":"schedule", "key":"schedule", "align":"center"},
        {"title":"操作", "dataIndex":"operation", "key":"operation", "align":"center", "width":100}
      ],
      tableData: [],
      total: 0
    }
  },
  mounted: function () {
    this.formFinish();
  },
  methods: {
    getPopupContainer() {
      return document.querySelector(".selectPosition")
    },
    queryStepDetail: async function (id) {
      const rs = await getStep(id);
      if (rs.code !== 200) return Message.error({ text: '获取项目过程明细失败：' + rs.msg });
      this.itemDetail = rs.data;
      this.producesList = rs.produces;
      this.resourceList = rs.resource;
      this.solutionsList = rs.solutions;
    },
    formQuery: function () {
      this.$refs.formRef.submit();
    },
    formReset: function () {
      this.$refs.formRef.reset();
    },
    queryList: async function () {
      this.isDataLoading = true;
      const rs = await listStep(this.queryForm);
      if (rs.code !== 200) return Message.error({ text: '获取项目过程列表信息失败：' + rs.msg });
      this.tableData = rs.rows;
      this.total = rs.total;
      // console.log("rs: ", rs);
      this.isDataLoading = false;
    },
    formFinish: async function (formValues) {
      this.queryForm = {
        pageNum: 1,
        pageSize: 10,
        itemNumber: formValues ? formValues.itemNumber : '',
        itemName: formValues ? formValues.itemName : '',
        itemType: formValues ? formValues.itemType : '',
        itemDegree: formValues ? formValues.itemDegree : '',
        dbType: formValues ? formValues.dbType : ''
      };
      this.queryList();
    },
    handleTableChange: function (p, filters) {
      const { current, pageSize } = p;
      this.queryForm.pageNum = current;
      this.queryForm.pageSize = pageSize;
      this.queryList();
      console.log('filters', p, filters);
    },
    tblOptForm: async function (record) {
      this.formDialogInfo.itemId = record ? record.itemId : 0;
      this.formDialogInfo.currRow = record ? record : {};
      this.formDialogInfo.title = '项目进度';

      await this.queryStepDetail(record.itemId);
      this.formDialogInfo.visible = true;
    },
    formDialogCancel: function () {
      this.formDialogInfo.visible = false;
      this.formFinish();
    }
  }
}
</script>

<style scoped>
  .w {
    /* height: 100%; */
    background-color: #F9FAFB;
  }

  /* 标题 */
  .title-area {
    height: 36px;
    background-color: #E9EBF0;
  }
  .title-area span {
    display: inline-block;
    font-weight: 500;
    line-height: 36px;
    margin-left: 15px;
  }

  /* 主体内容显示区 */
  .content-area {
    /* background-color: lightblue; */
    height: 100%;
    margin-left: 25px;
    margin-right: 25px;
    padding-bottom: 15px;
  }

  /* 查询表单区 */
  .form-area {
    /* background-color: lightcyan; */
    margin-top: 15px;
    margin-left: 50px;
    margin-right: 50px;
  }

  .uni-divider {
    margin: 0;
  }

  /* 按钮区 */
  .button-area {
    margin-top: 15px;
    /* background-color: lightcyan; */
  }
  .button-area svg {
    margin-top: 3px;
    margin-right: 5px;
  }

  /* 列表区 */

  /* 弹框区 */
  ::v-deep .fromModal > .uni-modal-wrap > .uni-modal-wrap-content > .uni-modal-footer {
    padding: 0;
  }
</style>
